#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;

int main()
{
    string s;
    while (cin >> s)
    {
        int hash[256] = { 0 };
        for (auto ss : s)
            hash[ss]++;

        priority_queue<int, vector<int>, greater<int> > h;
        for (int i = 0; i < 256; i++)
            if (hash[i])
                h.push(hash[i]);

        int ans = 0;
        while (h.size() > 1)
        {
            int a = h.top(); h.pop();
            int b = h.top(); h.pop();
            ans += a + b;
            h.push(a + b);
        }
        cout << ans << endl;
    }

    return 0;
}
